package com.tbit.common.entity.view;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 分页相关的封装对象
 *
 * @Author:BUGTian
 * @DATE: 2022/1/11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {
    /**
     * 当前页码
     *
     * @mock 1
     */
    private Integer page;

    /**
     * 每页显示的行
     *
     * @mock 10
     */
    private Integer size;

    /**
     * 总记录数
     *
     * @mock 1
     */
    private Long total;

    /**
     * 总页数
     *
     * @mock 1
     */
    private Integer totalPages;

    /**
     * 当前页记录
     */
    private List<T> list;

    public static <S, R> PageResult<R> convertPage(PageInfo<S> pageInfo, Class<R> clz, CopyOptions copyOptions) {
        List<R> rList = BeanUtil.copyToList(pageInfo.getList(), clz, copyOptions);
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), rList);
    }

    public static <S, R> PageResult<R> convertPage(PageInfo<S> pageInfo, Class<R> clz) {
        List<R> rList = BeanUtil.copyToList(pageInfo.getList(), clz);
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), rList);
    }

    public static <T> PageResult<T> convertPage(PageInfo<T> pageInfo) {
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    public static <T> PageResult<T> convertPage(Page<T> pageInfo) {
        return new PageResult<>((int) pageInfo.getCurrent(),
                (int) pageInfo.getSize(),
                pageInfo.getTotal(),
                (int) pageInfo.getPages(),
                pageInfo.getRecords());
    }

    public static <S, R> PageResult<R> convertPage(PageResult<S> pageInfo, Class<R> clz) {
        List<R> rList = BeanUtil.copyToList(pageInfo.getList(), clz);
        return new PageResult<>(pageInfo.getPage(), pageInfo.getSize(), pageInfo.getTotal(), pageInfo.getTotalPages(), rList);
    }
}
